﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.Share.Controls;
using System.Net;
using System.IO;

namespace CDTM.Modules.DANHMUC.HOME.Controls
{
    public partial class ItemManager : ModulePage
    {
        private static List<Components.DM> _listUsers = new List<Components.DM>();

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!PortalSecurity.HasManageAuthorization(this.ModuleID))
            //{
            //    PortalSecurity.AccessDenied();
            //}
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {

            Guid modeID = new Guid(this.Settings["MODE.ModuleID"].ToString());

            if (modeID == Guid.Empty)
            {
                ContentPane.Controls.Add(new LiteralControl("Chưa cấu hình ModuleID"));
                return;
            }

            BuildPageLink();

            if (Request.QueryString["categoryid"] != null)
            {
                _listUsers = LoadData(modeID, Convert.ToInt32(Request.QueryString["categoryid"])).OrderBy(x => x.SortOrder).ToList();
            }
            else
            {
                _listUsers = Components.DM.SelectCollectionAll(modeID).OrderBy(x => x.SortOrder).ToList();
            }

            gvMain.DataSource = _listUsers;
            gvMain.DataBind();
        }

        private void BuildPageLink()
        {
            if (Request.QueryString["CategoryID"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemManager, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        private List<Components.DM> LoadData(Guid moduleID, int categoryID)
        {
            List<Components.DM> list = new List<Components.DM>();

            IList<Category> allCategories = new List<Category>();
            allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ID == categoryID).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                list.AddRange(Components.DM.SelectCollectionBy(moduleID, topCategory.ID));

                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();

                foreach (Category childCategory in childCategories)
                {
                    list.AddRange(Components.DM.SelectCollectionBy(moduleID, childCategory.ID));
                }
            }

            return list;
        }

        protected void gvMain_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.DM.DeleteDM(Convert.ToInt32(gvMain.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void gvMain_PageIndexChanged(object sender, EventArgs e)
        {
            gvMain.SelectedIndex = -1;
        }

        protected void gvMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMain.PageIndex = e.NewPageIndex;
            LoadData();
        }

        public string GetDepartmentName(int categoryID)
        {
            Guid modeID = new Guid(this.Settings["MODE.ModuleID"].ToString());

            List<Category> list = Category.SelectCollectionAll(modeID).Where(x => x.ID == categoryID).ToList();

            return list.Count != 0 ? list[0].Title : "";
        }

        protected void gvMain_Load(object sender, EventArgs e)
        {
            gvMain.Columns[5].Visible = this.IsEditable;//Edit
            gvMain.Columns[6].Visible = this.IsDeleteable;//Delete
        }

        protected void gvMain_DataBound(object sender, EventArgs e)
        {
            HiddenField hdfID = null;
        }

        private string FixName(string nickName)
        {
            string sRet = nickName;

            if (!String.IsNullOrEmpty(nickName))
            {
                //Xóa các ký tự '
                //sRet = nickName.Replace("'", "");

                //Tìm kí tự $ trong tên Sheet
                int iLastIndex = sRet.LastIndexOf("@");
                if (iLastIndex != -1)//Tìm thấy, loại bỏ đoạn từ $ về cuối string
                    sRet = sRet.Substring(0, iLastIndex);
            }
            return sRet;
        }

    }
}